<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="rules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="slideTitle">
              <a-input v-model="model.slideTitle" placeholder="请输入轮播图标题"  ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="slideType" >
              <j-dict-select-tag
                v-model="model.slideType"
                dictCode="carousel_url" placeholder="请选择类型"
                @change="clearSelect"
              />
            </a-form-item>
          </a-col>

        </a-row>

        <a-row>

          <a-col :span="12">
            <a-form-model-item label="显示状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="showStatus">
              <j-dict-select-tag
                v-model="model.showStatus"
                dictCode="show_flag" placeholder="请选择显示状态"/>
            </a-form-model-item>
          </a-col>


            <a-col :span="12" v-if="this.model.slideType == 1">
              <a-form-item label="选择新闻" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="relationNewsId">
                <a-select placeholder="请选择新闻"   v-model="model.relationNewsId" >
                  <a-select-option
                                   :label="item.mainTitle"
                                   :value="item.id"
                                    v-for="item in commonNews"
                                   :key="item.id" >{{item.mainTitle}} </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <a-col :span="12" v-if="this.model.slideType == 2">
              <a-form-item label="选择公告" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="relationNewsId">
                <a-select placeholder="请选择公告"  v-model="model.relationNewsId">
                  <a-select-option
                                   :label="item.titile"
                                   :value="item.id"
                                   v-for="item in announceMent"
                                   :key="item.id" >{{item.titile}} </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <a-col :span="12" v-if="this.model.slideType == 3">
              <a-form-model-item label="跳转路径" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="slideUrl">
                <a-input v-model="model.slideUrl" placeholder="请输入跳转路径"  ></a-input>
              </a-form-model-item>
            </a-col>
        </a-row>

        <a-row >
            <a-col :span="12">
            <a-form-model-item label="排序值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sortNum">
              <a-input-number v-model="model.sortNum" placeholder="请输入排序值" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="轮播图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="slideImage">
              <j-image-upload class="avatar-uploader" text="添加主图" v-model="model.slideImage" ></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>



      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import {selectNews,selectAnno} from '@/api/app/mipSlideShow'

  export default {
    name: 'MipSlideShowForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        //存储查到的公共新闻
        commonNews:[],
        //存储符合添加的系统公告
        announceMent:[],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },

        rules: {
          slideTitle: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          slideType: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          showStatus: [{ required: true, message: '显示状态不能为空', trigger: 'blur' }],

          relationNewsId: [{ required: true, message: '关联属性不能为空', trigger: 'blur' }],

          slideUrl: [ { required: true, message: '跳转路径不能为空', trigger: 'blur' }],

          sortNum: [{ required: true, message: '排序不能为空', trigger: 'blur' }],

          slideImage: [{ required: true, message: '图不能为空', trigger: 'blur' }]
        },

        url: {
          add: "/app/mipSlideShow/add",
          edit: "/app/mipSlideShow/edit",
          queryById: "/app/mipSlideShow/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },

    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
       //获取新闻
      this.getNews();
       //获取通告
      this.getAnno();
    },
    methods: {
      //类型发生改变时，清空下拉框的值
      clearSelect(val){
       if(val == 1 || val== 2){
         this.model.slideUrl='';
       }else{
         this.model.relationNewsId='';
       }
     },

      getNews(){
        selectNews().then(res =>{
          this.commonNews=res.result.records||res.result;
          console.log(this.commonNews);
        });

      },

      getAnno(){
        selectAnno().then(res =>{
          this.announceMent=res.result.records||res.result;
          console.log(this.announceMent);
        });

      },



      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>